package com.rogue.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rogue.system.domain.dto.RoleDTO;
import com.rogue.system.domain.entity.SysMenu;
import com.rogue.system.domain.entity.SysRole;
import com.rogue.system.domain.entity.SysRoleMenu;
import com.rogue.system.domain.entity.SysUserRole;
import com.rogue.system.domain.vo.MenuVo;
import com.rogue.system.mapper.SysMenuMapper;
import com.rogue.system.mapper.SysRoleMapper;
import com.rogue.system.mapper.SysRoleMenuMapper;
import com.rogue.system.mapper.SysUserRoleMapper;
import com.rogue.system.service.SysRoleMenuService;
import com.rogue.system.service.SysRoleService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author Yeah
* @description 针对表【hs_role(用户角色表)】的数据库操作Service实现
* @createDate 2022-03-16 14:20:29
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService {

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public Boolean setMenuForRole(RoleDTO roleDTO) {
        List<SysRoleMenu> sysRoleMenuList = roleDTO.getMenuIds().stream().map(menuId -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleDTO.getId());
            return sysRoleMenu;
        }).collect(Collectors.toList());
        return sysRoleMenuService.saveBatch(sysRoleMenuList);
    }

    @Override
    public Boolean removeRole(Long id) {

        // 先删除 role
        int deleteRole = this.baseMapper.deleteById(id);
        // 在删除 role 与之关联 menu user
        int deleteRoleMenu = sysRoleMenuMapper.delete(
            new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
        int deleteRoleUser = sysUserRoleMapper.delete(
            new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
        if (deleteRole > 0 && deleteRoleMenu >0 && deleteRoleUser > 0){
            return true;
        }

        return false;
    }

    @Override
    public PageInfo<SysRole> listRole(RoleDTO roleDTO) {

        // 分页查询
        PageInfo<SysRole> pageInfo = PageHelper.startPage(roleDTO.getPage(),
                roleDTO.getCount())
            .doSelectPageInfo(() -> this.baseMapper.selectList(new QueryWrapper<SysRole>().lambda()
                    .eq(StrUtil.isNotBlank(roleDTO.getRoleCode()),SysRole::getRoleCode,roleDTO.getRoleCode())
                    .like(StrUtil.isNotBlank(roleDTO.getRoleName()),SysRole::getRoleName,roleDTO.getRoleName())
                    .eq(Objects.nonNull(roleDTO.getId()), SysRole::getId,roleDTO.getId())));
        return pageInfo;
    }

    @Override
    public SysRole getByUserId(Long id) {

        SysUserRole userRole = sysUserRoleMapper.selectOne(
            new QueryWrapper<SysUserRole>().eq("user_id", id));

        return this.baseMapper.selectById(userRole.getRoleId());
    }

    @Override
    public List<MenuVo> getMenuByRoleId(Long id) {
        // 先查出关联的菜单 id
        List<SysRoleMenu> list = sysRoleMenuMapper.selectList(
            new QueryWrapper<SysRoleMenu>().eq("role_id", id));
        // 提取菜单 id list
        List<Long> menuIds = list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        // 查出菜单具体属性
        List<SysMenu> sysMenuList = sysMenuMapper.selectBatchIds(menuIds);
        // 转换成 MenuVo 返回
        List<MenuVo> menuVos = CglibUtil.copyList(sysMenuList, MenuVo::new);
        menuVos.forEach(o -> o.setRoleId(id));

        return menuVos;

    }
}




